home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group97b.txt
/
000008_icon-group-sender _Wed Jul 2 23:18:27 1997.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
2KB
Received: from kingfisher.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Tue, 8 Jul 1997 08:43:06 MST
Received: by kingfisher.CS.Arizona.EDU; (5.65v3.2/1.1.8.2/08Nov94-0446PM)
id AA24626; Tue, 8 Jul 1997 08:43:06 -0700
Message-Id: <3.0.1.32.19970702231827.007ba100@alterdial.uu.net>
X-Sender: mail08621@alterdial.uu.net
X-Mailer: Windows Eudora Pro Version 3.0.1 (32)
Date: Wed, 02 Jul 1997 23:18:27 -0500
To: icon-group@cs.arizona.edu
From: Jerry Nowlin <nowlin@nowlin.com>
Subject: Re: A small puzzle
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Errors-To: icon-group-errors@cs.arizona.edu
Status: RO
> Write a procedure lcp(s1,s2) that returns the longest common
> prefix of strings s1 and s2. For example,
>
> write(lcp("fool","foodchain"))
>
> would output "foo", while
>
> write(lcp("aardvark","elephant"))
>
> would output "" (i.e. the null string).
I couldn't resist. I have an Icon solution:
# the longest common prefix procedure
procedure lcp(a,b); return a ? tab(match(b[1:*b to 0 by -1])); end
I also have a (gasp!) perl solution:
# the longest common prefix subroutine
sub lcp { do { return $_[0] if ($_[1] =~ m/^$_[0]/); }
while(chop($_[0])); }
I didn't attempt to optimize either to use the shortest string as the
pattern, but that would make both more efficient and is trivial to do.
It's not as clean looking though and wouldn't fit on one line. I tested
both of these on win95 and got the same results. I don't have a good way
to compare speed or efficiency though. I reckon Icon wins since it took
less characters :-)
Jerry Nowlin
Jerry Nowlin
nowlin@nowlin.com
http://www.xnet.com/~nowlin/